package cn.jinbyte.data.changelog.wapper;


import cn.jinbyte.data.changelog.ChangeType;
import cn.jinbyte.data.changelog.annotation.EntityLogProp;
import lombok.Getter;
import lombok.experimental.SuperBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * 子属性内容差异包装器
 */
@Getter
@SuperBuilder
public class EntityChangeWrapper extends ChangeWrapper {
    /**
     * 子属性类型
     */
    Class<?> subClass;
    /**
     * 子属性差异内容
     */
    List<ChangeWrapper> wrappers;

    public List<String> toLogString(ChangeType cType) {
        if (wrappers == null) {
            return new ArrayList<>();
        }
        List<String> logStrings = new ArrayList<>(wrappers.size());
        EntityLogProp entityLogProp = propField.getAnnotation(EntityLogProp.class);
        String name = entityLogProp.name();
        for (ChangeWrapper wrapper : wrappers) {
            if (wrapper instanceof PropChangeWrapper propChangeWrapper) {
                logStrings.add(propChangeWrapper.toLogString(cType));
            }
            if (wrapper instanceof EntityChangeWrapper entityChangeWrapper) {
                logStrings.addAll(entityChangeWrapper.toLogString(cType));
            }
        }
        return logStrings;
    }
}